<?php
// +----------------------------------------------------------------------
// | 品达网络 [ 品质保证，助企腾达 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.pdwl.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 萧涩啊 <wangmode@163.com>
// +----------------------------------------------------------------------
// | 支出管理

namespace app\models;
use think\Exception;



class Expend extends BaseModel
{
    protected $table = 'sh_expend';
    /**
     * 支出列表
     * @param $page
     * @param $limit
     * @param int $department_id //部门id
     * @param int $use_id       //用途id
     * @param int $employee_id  //员工id
     * @param int $start_time
     * @param int $end_time
     * @return array|bool|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getList($page,$limit,$department_id=0,$use_id=0,$employee_id=0,$start_time=0,$end_time=0)
    {
        try {
            $where = [];
            if($start_time!==0){
                $where[] = ['e.expend_time','>=',$start_time];
            }
            if($end_time!==0){
                $where[] = ['e.expend_time','<',$end_time];
            }
            if(!empty($department_id)){
                $where[] = ['e.department_id','=',$department_id];
            }
            if(!empty($use_id)){
                $where[] = ['e.use_id','=',$use_id];
            }
            if(!empty($employee_id)){
                $where[] = ['e.employee_id','=',$employee_id];
            }
           return $this->alias('e')
               ->leftJoin('sh_expend_use u','e.use_id=u.id')
               ->leftJoin('sh_department d',"e.department_id=d.id")
               ->leftJoin('sh_employee em',"e.employee_id=em.id")
               ->where($where)
               ->limit(($page-1)*$limit,$limit)
               ->order('e.id desc')
               ->field('e.*,d.name as department,u.name as use_name,em.name as employee')
               ->select();
        }catch (Exception $exception){
            return self::setErrorInfo($exception->getMessage());
        }
    }

    /**
     * @param int $department_id
     * @param int $use_id
     * @param int $employee_id
     * @param int $start_time
     * @param int $end_time
     * @return float|int|string
     */
    public function getCount($department_id=0,$use_id=0,$employee_id=0,$start_time=0,$end_time=0)
    {
        $where = [];
        if($start_time!==0){
            $where[] = ['expend_time','>',$start_time];
        }
        if($end_time!==0){
            $where[] = ['expend_time','<',$end_time];
        }
        if($department_id!==0){
            $where[] = ['department_id','=',$department_id];
        }
        if($use_id!==0){
            $where[] = ['use_id','=',$use_id];
        }
        if($employee_id!==0){
            $where[] = ['employee_id','=',$employee_id];
        }
        return $this->where($where)->count();
    }


}
